package com.encyclopedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.encyclopedia.entity.Family;
import com.encyclopedia.mapper.FamilyMapper;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.FamilyService;
import com.encyclopedia.service.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class FamilyServiceImpl implements FamilyService {
    @Autowired
    private FamilyMapper familyMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<Family> getFamilyAll() {
        String key = "FamilyServiceImplFamilyAll";
        List<Family> familyRedis = (List<Family>) redisUtils.get(key);
        if (familyRedis != null) return familyRedis;
        List<Family> families = familyMapper.selectList(null);
        redisUtils.set(key, families, 60 * 60 * 12);
        if (families.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);
        return families;
    }

    //todo遇到的一问题，怎么在添加之后删除所有的family缓存？
    @Override
    public Integer addFamily(Family family) {
        int insert = familyMapper.insert(family);
        if (insert == 0) throw new ServiceException(RequestException.INSERT_ERROR);

        return insert;
    }

    @Override
    public Integer updateFamily(Family family) {
        int i = familyMapper.updateById(family);
        if (i == 0) throw new ServiceException(RequestException.UPDATE_ERROR);

        return i;
    }

    @Override
    public Family getFamilyOne(Family family) {
        if (family == null) throw new ServiceException(RequestException.Family_ParamNull);
        String key = "FamilyServiceImplFamilyOne";
        Family familyOne = null;
        if (family.getFamilyId() != null) {
            key += "familyId:" + family.getFamilyId();
            familyOne = (Family) redisUtils.get(key);
            if (familyOne != null) return familyOne;
            familyOne = familyMapper.selectById(family.getFamilyId());
            redisUtils.set(key, familyOne, 60 * 60 * 12);
            if (familyOne == null) throw new ServiceException(RequestException.NOT_FOUND);
        } else if (family.getFamilyName() != null) {
            key += "familyName:" + family.getFamilyName();
            familyOne = (Family) redisUtils.get(key);
            if (familyOne != null) return familyOne;
            familyOne = familyMapper.selectOne(new LambdaQueryWrapper<Family>().eq(Family::getFamilyName, family.getFamilyName()));
            redisUtils.set(key, familyOne, 60 * 60 * 12);
            if (familyOne == null) throw new ServiceException(RequestException.NOT_FOUND);
        }
        return familyOne;

    }

    @Override
    public List<Family> getFamilyByName(String familyName) {
        if (familyName == null) throw new ServiceException(RequestException.Family_ParamNull);
        String key = "FamilyServiceImplFamilyByName:" + familyName;
        List<Family> familyRedis = (List<Family>) redisUtils.get(key);
        if (familyRedis != null) return familyRedis;
        List<Family> families = familyMapper.selectList(new LambdaQueryWrapper<Family>().like(Family::getFamilyName, familyName));
        redisUtils.set(key, families, 60 * 60 * 12);
        if (families.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);
        return families;

    }

}
